<template>
  <div id="app">
    <el-button type="primary" plain @click="add()">新增</el-button>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="stuNo" label="学号" width="200"></el-table-column>
      <el-table-column prop="name" label="学生姓名" width="200"></el-table-column>
      <el-table-column prop="chinese" label="语文" width="200"></el-table-column>
      <el-table-column prop="mathematics" label="数学" width="200"></el-table-column>
      <el-table-column prop="english" label="英语" width="200"></el-table-column>
      <el-table-column prop="physics" label="物理" width="200"></el-table-column>
      <el-table-column prop="chemistry" label="化学" width="200"></el-table-column>
      <el-table-column prop="biology" label="生物" width="200"></el-table-column>
      <el-table-column prop="sum" label="总成绩" width="200"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="updateClick(scope.row)" type="text" size="small">修改</el-button>
          <el-button type="text" size="small" @click="delClick(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNo"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="80px">
        <el-form-item label="学号">
          <el-input v-model="form.stuNo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="科目" v-for="(item,index) in this.form.houseList">
          <el-col :span="11">
            <el-input v-model="item.courseName" autocomplete="off"></el-input>
          </el-col>
          <el-col class="line" :span="2">分数</el-col>
          <el-col :span="7">
            <el-input v-model="item.fraction" autocomplete="off"></el-input>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" plain @click="addHouseList()">新增</el-button>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" plain @click="delHouseList(index)">删除</el-button>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  mounted() {
    this.getReuqest();
  },
  methods: {
    delHouseList(index) {
      this.form.houseList.splice(index, 1);
    },
    addHouseList() {
      this.form.houseList.push({ courseName: "", fraction: null });
    },
    add() {
      this.title = "添加";
      this.dialogFormVisible = true;
      this.initStu();
    },
    initStu() {
      this.form.id = null;
      this.form.stuNo = "";
      this.form.name = "";
      this.form.houseList = [{ courseName: "", fraction: null }];
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getReuqest();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNo = val;
      this.getReuqest();
    },
    updateClick(row) {
      console.log(row);
      this.title = "修改";
      this.dialogFormVisible = true;
      this.form.name = row.name;
      this.form.stuNo = row.stuNo;
      this.form.id = row.id;
      this.form.houseList = [];
      if(row.chinese>0){
          this.form.houseList.push({"courseName": "语文", "fraction": row.chinese});
      }
      if(row.mathematics>0){
          this.form.houseList.push({"courseName": "数学", "fraction": row.chinese});

      }
      if(row.english>0){
        this.form.houseList.push({"courseName": "英语", "fraction": row.chinese});

      }
      if(row.biology>0){
          this.form.houseList.push({"courseName": "生物", "fraction": row.chinese});

      }

      if(row.chemistry>0){
          this.form.houseList.push({"courseName": "化学", "fraction": row.chinese});

      }
      if(row.physics>0){
        this.form.houseList.push({"courseName": "物理", "fraction": row.chinese});
      }

      
    },
    delClick(row){
      console.log(row);
       var $this = this;
      this.axios({
        method: "delete",
        url: "http://localhost:8081/tStudent/delete",
        params:{"stuID":row.id}
      }).then(function(res) {
        console.log("res---->>>", res);
        alert(res.data.msg);
        if(res.data.code==200){
          $this.dialogFormVisible = false;
          $this.getReuqest();
        }
      });
    },
    saveOrUpdate() {
      var $this = this;
      this.axios({
        method: "post",
        url: "http://localhost:8081/tStudent/saveOrUpdate",
        data:this.form
      }).then(function(res) {
        console.log("res---->>>", res);
        alert(res.data.msg);
        if(res.data.code==200){
          $this.dialogFormVisible = false;
          $this.getReuqest();
        }
      });
    },
    getReuqest() {
      var $this = this;
      this.axios({
        method: "get",
        url: "http://localhost:8081/tStudent/page/list",
        params: {
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }
      }).then(function(res) {
        console.log("res---->>>", res);
        $this.tableData = res.data.obj.list;
        $this.total = res.data.obj.total;
      });
    }
  },

  data() {
    return {
      tableData: [],
      total: 100,
      pageSize: 2,
      pageNo: 1,
      dialogFormVisible: false,
      title:"添加",
      form: {
        id:null,
        stuNo: "",
        name: "",
        houseList: [{ courseName: "", fraction: null }]
      }
    };
  }
};
</script>